package com.qclix.inv.xml;

import com.qclix.inv.Client;
import com.qclix.inv.Invoice;
import com.qclix.inv.InvoiceLine;

import java.util.Collections;
import java.util.List;

/**
 * Basic Implementation of DomaintoXml and XmlToDomain translation
 */
public class DfltObjectTranslatorImpl implements DomainToXMLTranslator, XMLtoDomainTranslator {

    // TODO use a library to map the beans

    public XMLInvoice getXMLversion(Invoice invoice) {
        XMLInvoice xmlVersion = new XMLInvoice();
        xmlVersion.setIssueDate(invoice.getIssueDate());
        xmlVersion.setNumber(invoice.getNumber());
        xmlVersion.setClient(getXMLversion(invoice.getClient()));
        XMLInvoiceLine xmlLine;
        int idx = 0;
        for (InvoiceLine line: invoice.getLines()) {
            xmlLine = getXMLversion(line);
            xmlLine.setIdx(idx++);
            xmlVersion.getLines().add(xmlLine);
        }
        return xmlVersion;
    }

    public XMLInvoiceLine getXMLversion(InvoiceLine line) {
        XMLInvoiceLine xmlVersion = new XMLInvoiceLine();
        xmlVersion.setId(line.getId());
        xmlVersion.setDescription(line.getDescription());
        xmlVersion.setQuantity(line.getQuantity());
        xmlVersion.setUnitPrice(line.getUnitPrice());
        xmlVersion.setVat(line.getVat());
        return xmlVersion;
    }

    public XMLClient getXMLversion(Client client) {
        XMLClient xmlVersion = new XMLClient();
        xmlVersion.setId(client.getId());
        xmlVersion.setName(client.getName());
        xmlVersion.setStreet1(client.getStreet1());
        xmlVersion.setStreet2(client.getStreet2());
        xmlVersion.setZip(client.getZip());
        xmlVersion.setCity(client.getCity());
        xmlVersion.setCountry(client.getCountry());
        xmlVersion.setVat(client.getVat());
        return xmlVersion;
    }

    public Invoice getDomainVersion(XMLInvoice invoice) {
        Invoice domainVersion = new Invoice();
        domainVersion.setIssueDate(invoice.getIssueDate());
        domainVersion.setNumber(invoice.getNumber());
        domainVersion.setClient(getDomainVersion(invoice.getClient()));
        List<XMLInvoiceLine> xmlLines = invoice.getLines();
        Collections.sort(invoice.getLines(),new LineIndexComparator());
        for (XMLInvoiceLine line: xmlLines) {
            domainVersion.getLines().add(getDomainVersion(line));
        }
        return domainVersion;
    }

    public InvoiceLine getDomainVersion(XMLInvoiceLine line) {
        InvoiceLine domainVersion = new InvoiceLine(line.getDescription(), line.getQuantity(), line.getUnitPrice(), line.getVat());
        domainVersion.setId(line.getId());
        return domainVersion;
    }

    public Client getDomainVersion(XMLClient client) {
        Client domainVersion = new Client();
        domainVersion.setId(client.getId());
        domainVersion.setName(client.getName());
        domainVersion.setStreet1(client.getStreet1());
        domainVersion.setStreet2(client.getStreet2());
        domainVersion.setZip(client.getZip());
        domainVersion.setCity(client.getCity());
        domainVersion.setCountry(client.getCountry());
        domainVersion.setVat(client.getVat());
        return domainVersion;
    }
}
